﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Com.Framework;
using Com.Framework.Data;
using Com.Framework.Enc;
using Com.Library.DB.User;

namespace Com.Web.coffinegurunaru.popup
{
	public partial class pop_pwsearch_1 : System.Web.UI.Page
	{
		public UserEntity UserInfo
		{
			get;
			set;
		}

		public string UserName
		{
			get;
			set;
		}

		public string UserID
		{
			get;
			set;
		}

		public string Email
		{
			get;
			set;
		}

		public string GetPassword()
		{
			string seedData = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
			string pwddata = string.Empty;
			System.Random rnd = new Random(DateTime.Now.Second);
			for (int i = 0; i < 6; i++)
			{
				int loc = rnd.Next(0, seedData.Length);
				pwddata += seedData[loc];
			}

			return pwddata;
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			UserName = Request.Form["UserName"].Parse(string.Empty);
			UserID = Request.Form["UserID"].Parse(string.Empty);
			Email = Request.Form["Email"].Parse(string.Empty);
			string Answer = Request.Form["Answer"].Parse(string.Empty);

			if (UserName == string.Empty)
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), "loginerror", @"
<script language='javascript'>
alert('이름을 입력하세요.');
location.href='/popup/pop_pwsearch.aspx';
</script>
");
				return;
			}
			if (UserID == string.Empty)
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), "loginerror", @"
<script language='javascript'>
alert('아이디를 입력하세요.');
location.href='/popup/pop_pwsearch.aspx';
</script>
");
				return;
			}
			if (Email == string.Empty)
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), "loginerror", @"
<script language='javascript'>
alert('이메일을 입력하세요.');
location.href='/popup/pop_pwsearch.aspx';
</script>
");
				return;
			}

			UserInfo = UserModel.UserInfo(UserID);
			if (UserInfo.UserNo == 0)
			{
				Response.Redirect("/popup/pop_pwsearch_2.aspx");
				Response.End();
			}

			if (UserInfo.Email.ToLower() != Email.ToLower() || UserInfo.UserName != UserName)
			{
				Response.Redirect("/popup/pop_pwsearch_2.aspx");
				Response.End();
			}

			if (this.IsPostBack)
			{
				if (Answer != string.Empty)
				{
					if (Answer != UserInfo.Answer)
					{
						Response.Redirect("/popup/pop_pwsearch_2.aspx");
						Response.End();
					}
					else
					{
						string UserPWD = GetPassword();
						string Content = @"안녕하세요. " + UserInfo.UserName + "(" + UserInfo.UserID + @") 회원님!<Br /><Br />
문의하신 회원님의 임시 비밀번호는 " + UserPWD + @" 입니다.<Br />
임시로 발급된 비밀번호입니다. 로그인 후 비밀번호를 변경하여 사용해 주시기 바랍니다";
						Com.Framework.Email.WebMail mail = new Com.Framework.Email.WebMail();
						mail.Send("manager@gurunaru2.cafe24.com", UserInfo.Email, "문의하신 비밀번호 정보입니다.", Content);

						ClientScript.RegisterClientScriptBlock(this.GetType(), "loginerror", @"
<script language='javascript'>
alert('새로운 비밀번호를 이메일로 발송하였습니다.');
self.close();
</script>
");
						UserModifyArguments arg = new UserModifyArguments();
						arg.n4UserNo = UserInfo.UserNo;
						arg.strMD5PWD = Crypt.MD5HashCrypt(UserPWD);
						UserModel.Modify(arg);
					}
				}
			}
		}
	}
}
